using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace TG
{
    public sealed class GameConfigHelper
    {

        public static Queue<BaseGameConfig> gameConfigQueue = new Queue<BaseGameConfig>();

        /// <summary>
        /// 减速比
        /// </summary>
        public static void LoadJianSuBi()
        {
            var iniParser = new INIParser();
            //string[] configs = File.ReadAllLines(Application.streamingAssetsPath + $"/configOfPH.txt");
            iniParser.Open(Application.streamingAssetsPath + $"/configOfPH.txt");
            EtherCAT.Instance.standReductRatioX = iniParser.ReadValue(Default.PH_NAME, "standReductRatioX", 480);
            EtherCAT.Instance.standReductRatioY = iniParser.ReadValue(Default.PH_NAME, "standReductRatioY", 100);
            EtherCAT.Instance.seatReductRatioX = iniParser.ReadValue(Default.PH_NAME, "seatReductRatioX", 300);
            EtherCAT.Instance.seatReductRatioY = iniParser.ReadValue(Default.PH_NAME, "seatReductRatioY", 100);
        }

        public static void LoadAllGameConfig(BaseGameConfig baseGameConfig)
        {
            var iniParser = new INIParser();
            var files = GetGameConfigFiles();
            foreach (var t in files)
            {
                iniParser.Open(t);
                SetBaseConfig(iniParser, baseGameConfig);
                iniParser.Close();
            }
        }

        private static void SetBaseConfig(INIParser iniParser, BaseGameConfig curGameConfig)
        {
            curGameConfig.playName = iniParser.ReadValue(Default.SECTION_NAME, "姓名", "");
            curGameConfig.playAge = iniParser.ReadValue(Default.SECTION_NAME, "年龄", "");
            curGameConfig.playHeight = iniParser.ReadValue(Default.SECTION_NAME, "身高", 0);
            curGameConfig.playWeight = iniParser.ReadValue(Default.SECTION_NAME, "体重", 0);

            curGameConfig.baseADNodeID = iniParser.ReadValue(Default.SECTION_NAME, "底座AD节点ID", "");
            curGameConfig.chairADNodeID = iniParser.ReadValue(Default.SECTION_NAME, "座椅AD节点ID", "");
            curGameConfig.isTest = iniParser.ReadValue(Default.SECTION_NAME, "测试", false);
            curGameConfig.posture = iniParser.ReadValue(Default.SECTION_NAME, "姿态", "") == "站姿"
                ? Posture.Stand
                : Posture.Sit;

            switch (iniParser.ReadValue(Default.SECTION_NAME, "游戏难度", ""))
            {
                case "简单":
                    curGameConfig.difficulty = 1;
                    break;
                case "困难":
                    curGameConfig.difficulty = 2;
                    break;
                default:
                    curGameConfig.difficulty = 2;
                    break;
            }

            curGameConfig.gameType = iniParser.ReadValue(Default.SECTION_NAME, "游戏模式", "") == "闯关"
                ? 0
                : 1;
            
            curGameConfig.gameTime = iniParser.ReadValue(Default.SECTION_NAME, "游戏时间(分钟)", 10);

            switch (iniParser.ReadValue(Default.SECTION_NAME, "晃动灵敏度", ""))
            {
                case "低":
                    curGameConfig.sensitivity = SensitivityType.Low;
                    break;
                case "中":
                    curGameConfig.sensitivity = SensitivityType.Middle;
                    break;
                case "高":
                    curGameConfig.sensitivity = SensitivityType.High;
                    break;
            }

            curGameConfig.isFirstView =
                iniParser.ReadValue(Default.SECTION_NAME, "视角", "").Equals("第一人称") ? true : false;
            curGameConfig.musicValue = iniParser.ReadValue(Default.SECTION_NAME, "音乐", "");
            curGameConfig.tipMusicValue = iniParser.ReadValue(Default.SECTION_NAME, "提示", "");
            curGameConfig.gravityX = iniParser.ReadValue(Default.SECTION_NAME, "重心坐标X", 0);
            curGameConfig.gravityY = iniParser.ReadValue(Default.SECTION_NAME, "重心坐标Y", 0);
            curGameConfig.sensor1 = iniParser.ReadValue(Default.SECTION_NAME, "传感器1", 0);
            curGameConfig.sensor2 = iniParser.ReadValue(Default.SECTION_NAME, "传感器2", 0);
            curGameConfig.sensor3 = iniParser.ReadValue(Default.SECTION_NAME, "传感器3", 0);
            curGameConfig.sensor4 = iniParser.ReadValue(Default.SECTION_NAME, "传感器4", 0);
            curGameConfig.initiative.SetValue(iniParser.ReadValue(Default.SECTION_NAME, "主动", false),
                iniParser.ReadValue(Default.SECTION_NAME, "主动-阻尼等级", 0),
                iniParser.ReadValue(Default.SECTION_NAME, "主动-横轴最大翻转角度", 0),
                iniParser.ReadValue(Default.SECTION_NAME, "主动-纵轴最大翻转角度", 0));
            curGameConfig.rolloverModel = iniParser.ReadValue(Default.SECTION_NAME, "翻转模式", 0);
            curGameConfig.passive.SetValue(iniParser.ReadValue(Default.SECTION_NAME, "被动", false),
                iniParser.ReadValue(Default.SECTION_NAME, "被动-阻尼等级", 0),
                iniParser.ReadValue(Default.SECTION_NAME, "被动-横轴最大翻转角度", 0),
                iniParser.ReadValue(Default.SECTION_NAME, "被动-纵轴最大翻转角度", 0));
            curGameConfig.tutorial = iniParser.ReadValue(Default.SECTION_NAME, "游戏教程演示", false);
        }

        public static string[] GetGameConfigFiles()
        {
            string tmpPath;
#if UNITY_EDITOR
            tmpPath = Application.dataPath + $"/GameConfig";
#else
            tmpPath = Application.dataPath.Replace(Application.productName + "_Data", string.Empty);
            Debug.LogWarning(tmpPath);
#endif
            Debug.LogError("tmpPath = " + tmpPath);
            string[] files = Directory.GetFiles(tmpPath, "game_*.txt");
            Array.Sort(files, (a, b) =>
            {
                int indexA = int.Parse(Path.GetFileNameWithoutExtension(a).Substring(5));
                int indexB = int.Parse(Path.GetFileNameWithoutExtension(b).Substring(5));
                return indexA.CompareTo(indexB);
            });
            return files;

        }

    }
}